package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.AdminDaliy;
import com.ruoyi.system.service.IAdminDaliyService;
import com.ruoyi.web.vo.AdminDaliyView;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 日报Controller
 * 
 * @author ruoyi
 * @date 2020-12-31
 */
@Controller
@RequestMapping("/system/daliy")
public class AdminDaliyController extends BaseController
{
    private String prefix = "system/daliy";

    @Autowired
    private IAdminDaliyService adminDaliyService;

    @RequiresPermissions("system:daliy:view")
    @GetMapping()
    public String daliy(ModelMap mmap)
    {
        Date workTomorrow = DateUtils.getWorkTomorrow();
        mmap.put("workTomorrow", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, workTomorrow));
        mmap.put("today", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, DateUtils.getNowDate()));
        SysUser currentUser = ShiroUtils.getSysUser();
        mmap.put("currentUserId", currentUser.getUserId());
        return prefix + "/daliy";
    }

    @RequiresPermissions("system:daliy:view")
    @GetMapping("/statistics")
    public String statistics(ModelMap mmap)
    {
        Date workTomorrow = DateUtils.getWorkTomorrow();
        mmap.put("workTomorrow", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, workTomorrow));
        return prefix + "/statistics";
    }



    /**
     * 查询日报列表
     */
    @RequiresPermissions("system:daliy:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AdminDaliyView adminDaliyView)
    {
        startPage();
        List<AdminDaliyView> list = adminDaliyService.selectAdminDaliyList(adminDaliyView);
        return getDataTable(list);
    }

    /**
     * 查询日报统计列表
     */
    @RequiresPermissions("system:daliy:list")
    @PostMapping("/statistics")
    @ResponseBody
    public TableDataInfo statistics(AdminDaliyView adminDaliyView)
    {
        startPage();
        List<AdminDaliyView> list = adminDaliyService.selectAdminDaliyListStatistics(adminDaliyView);
        return getDataTable(list);
    }

    /**
     * 导出日报列表
     */
    @RequiresPermissions("system:daliy:export")
    @Log(title = "日报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AdminDaliyView adminDaliyView)
    {
        List<AdminDaliyView> list = adminDaliyService.selectAdminDaliyList(adminDaliyView);
        ExcelUtil<AdminDaliyView> util = new ExcelUtil<AdminDaliyView>(AdminDaliyView.class);
        return util.exportExcel(list, "daliy");
    }
    /**
     * 导出日报列表
     */
    @RequiresPermissions("system:daliy:export")
    @Log(title = "日报", businessType = BusinessType.EXPORT)
    @PostMapping("/exportStatistics")
    @ResponseBody
    public AjaxResult exportStatistics(AdminDaliyView adminDaliyView)
    {
        List<AdminDaliyView> list = adminDaliyService.selectAdminDaliyList(adminDaliyView);
        ExcelUtil<AdminDaliyView> util = new ExcelUtil<AdminDaliyView>(AdminDaliyView.class);
        return util.exportExcel(list, "daliy");
    }

    /**
     * 新增日报任务
     */
    @GetMapping("/addTask/{num}")
    public String addTask(@PathVariable("num") Long num,ModelMap mmap)
    {
        mmap.put("num", num);
        return prefix + "/task";
    }
    /**
     * 新增日报
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存日报
     */
    @RequiresPermissions("system:daliy:add")
    @Log(title = "日报", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AdminDaliy adminDaliy)
    {
        String msg = adminDaliyService.insertAdminDaliy(adminDaliy);
        if (StringUtils.isNotEmpty(msg)) {
          return error(msg);
        }
        return success();
    }

    /**
     * 查看日报
     */
    @GetMapping("/detail/{daliyId}")
    public String detail(@PathVariable("daliyId") String daliyId, ModelMap mmap)
    {
        AdminDaliy adminDaliy = adminDaliyService.selectAdminDaliyById(daliyId);
        mmap.put("adminDaliy", adminDaliy);
        return prefix + "/detail";
    }

    /**
     * 修改日报
     */
    @GetMapping("/edit/{daliyId}")
    public String edit(@PathVariable("daliyId") String daliyId, ModelMap mmap)
    {
        AdminDaliy adminDaliy = adminDaliyService.selectAdminDaliyById(daliyId);
        mmap.put("adminDaliy", adminDaliy);
        return prefix + "/edit";
    }

    /**
     * 修改保存日报
     */
    @RequiresPermissions("system:daliy:edit")
    @Log(title = "日报", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AdminDaliy adminDaliy)
    {
        return toAjax(adminDaliyService.updateAdminDaliy(adminDaliy));
    }

    /**
     * 删除日报
     */
    @RequiresPermissions("system:daliy:remove")
    @Log(title = "日报", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(adminDaliyService.deleteAdminDaliyByIds(ids));
    }
}
